Stata+R:Stata 与 R 等效命令备忘录

您所在的位置:网站首页 stata tab命令 Stata+R:Stata 与 R 等效命令备忘录

Stata+R:Stata 与 R 等效命令备忘录

2024-01-08 20:35| 来源: 网络整理| 查看: 265

作者:任建辉(山西财经大学)邮箱:[email protected] 连享会-知乎推文列表

Note: 助教招聘信息请进入「课程主页」查看。

因果推断-内生性 专题 ⌚ 2020.11.12-15 主讲:王存同 (中央财经大学);司继春(上海对外经贸大学) 课程主页https://gitee.com/arlionn/YG | 微信版

http://qr32.cn/BlTL43 (二维码自动识别)

空间计量 专题 ⌚ 2020.12.10-13 主讲:杨海生 (中山大学);范巧 (兰州大学) 课程主页https://gitee.com/arlionn/SP | 微信版

https://gitee.com/arlionn/DSGE (二维码自动识别)

连享会最新专题直播 1.引言

「左手 Stata,右手 Python / R」,精通一个,掌握一些。

该备忘录总结了常见的 Stata 计量经济分析命令,并提供它们在 R 中的等效命令与之对应。更多关于导入/清理数据、变量转换和其他基本命令可参考Hanck等(2019)的《Econometrics with R》,以及 Wickham和Grolemund(2017)的《R for Data Science》。本示例选自 wooldridge《计量经济学导论:现代观点》,其中 Stata 数据集的下载链接为datasets, R 数据集可直接通过安装 wooldridge 包来获取,更加的方便。除了特别说明外,所有 R 命令都源自基础R包。在其后的每小节中,我们都是分两部分代码段来展开,前一段为 stata 代码块,后一段为等效的 R 代码块。

特别申明:资料来源为 https://github.com/rstudio/cheatsheets2.安装

注意:在stata中,一般主要依赖log文件来储存命令和结果输出,R却不然。在R中,通常使用由谢益辉编写的Rmarkdown语法创建R-markdown文件来捕获代码和结果输出。

stata代码块

ssc install outreg2 // 安装outreg2包。注意,stata安装包不需要每次使用时调用 // 在R中每次使用相应的包,需要输入library(packages name)来调用

R代码块

install.packages("wooldridge") #install `wooldridge` package data(package = "wooldridge") #list datasets in `wooldridge` package load(wage1) #load `wage1` dataset into session ?wage1 #consult documentation on `wage1` dataset连享会最新专题直播3.基本绘图

基础绘图部分主要演示了直方图、散点图、散点图加拟合线以及分组箱线图,示例数据为 wage1。

stata代码块

use http://fmwww.bc.edu/ec-p/data/wooldridge/wage1 hist(wage) //histogram of `wage`hist(wage), by(nonwhite) scatter (wage edu) //scatter plot of `wage` by `educ` twoway (scatter wage educ) (lfit wage educ) //scatter plot with fitted line graph box wage, by(nonwhite) //boxplot of wage by `nonwhite`

R代码块

library(wooldridge) // 其余部分R代码块的运行,都是提前加载wooldridge包,不再进一步重复。 hist(wage1$wage) # histogram of `wage`` plot(y = wage$1wage, x = wage1$educ) abline(lm(wage1$wage~wage1$educ),col=“red”) # add fitted line to scatterplot boxplot(wage1$wage~wage1$nonwhite) # boxplot of `wage` by `nonwhite`4.汇总数据

Stata的劣势是仅允许一个人每次使用一个数据集,在R中却可以同时调入多个数据集,因此必须在每个函数调用中指定。注意:R没有等同于Stata中codebook的命令。在R中,安装AER包时,会自动安装其他有用的附属包:car、lmtest、sandwich。

stata代码块

browse // open browser for loaded data describe // describe structure of loaded data summarize // display summary statistics for all variables in dataset list in 1/6 // display first 6 rows tabulate educ // tabulate `educ`variable frequencies tabulate educ female // cross-tabulate `educ` and `female` frequencies

R代码块

View(wage1) # open browser for loaded`wage1` data str(wage1) # describe structure of `wage1` data summary(wage1) # display summary statistics for `wage1` variables head(wage1) # display first 6 (default) rows data tail(wage1) # display last 6 rows table(wage1$educ) #tabulate `educ` frequencies table(“yrs_edu” = wage1$educ, “female” =wage1$female) # tabulate `educ`frequencies name table columns5.生成或编辑变量

本部分涉及生成新变量、计算变量的均值、选取部分变量、生成虚拟变量等相关内容

stata代码块

gen exper2 = exper^2 // create`exper` squared variable egen wage_avg = mean(wage) // create average wage variable drop tenursq // drop `tenursq`variable keep wage educ exper nonwhite // keep selected variables tab numdep, gen(numdep) // create dummy variables for `numdep` recode exper (1/20 = 1 "1 to 20 years") (21/40 = 2 "21 to 40 years") (41/max = 3 "41+ years"),gen(experlvl) // recode `exper` and gen new variable

R代码块

wage1$exper2


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3